/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvTravailprime;
import fr.cggveritas.crewintranet.jms.entities.CvTypeprime;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypeprimeJpaController implements Serializable {

    public CvTypeprimeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvTypeprime cvTypeprime) throws PreexistingEntityException, Exception {
        if (cvTypeprime.getCvTravailprimeList() == null) {
            cvTypeprime.setCvTravailprimeList(new ArrayList<CvTravailprime>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvTravailprime> attachedCvTravailprimeList = new ArrayList<CvTravailprime>();
            for (CvTravailprime cvTravailprimeListCvTravailprimeToAttach : cvTypeprime.getCvTravailprimeList()) {
                cvTravailprimeListCvTravailprimeToAttach = em.getReference(cvTravailprimeListCvTravailprimeToAttach.getClass(), cvTravailprimeListCvTravailprimeToAttach.getCvTravailprimePK());
                attachedCvTravailprimeList.add(cvTravailprimeListCvTravailprimeToAttach);
            }
            cvTypeprime.setCvTravailprimeList(attachedCvTravailprimeList);
            em.persist(cvTypeprime);
            for (CvTravailprime cvTravailprimeListCvTravailprime : cvTypeprime.getCvTravailprimeList()) {
                CvTypeprime oldCvTypeprimeOfCvTravailprimeListCvTravailprime = cvTravailprimeListCvTravailprime.getCvTypeprime();
                cvTravailprimeListCvTravailprime.setCvTypeprime(cvTypeprime);
                cvTravailprimeListCvTravailprime = em.merge(cvTravailprimeListCvTravailprime);
                if (oldCvTypeprimeOfCvTravailprimeListCvTravailprime != null) {
                    oldCvTypeprimeOfCvTravailprimeListCvTravailprime.getCvTravailprimeList().remove(cvTravailprimeListCvTravailprime);
                    oldCvTypeprimeOfCvTravailprimeListCvTravailprime = em.merge(oldCvTypeprimeOfCvTravailprimeListCvTravailprime);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvTypeprime(cvTypeprime.getTypeprime()) != null) {
                throw new PreexistingEntityException("CvTypeprime " + cvTypeprime + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypeprime cvTypeprime) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeprime persistentCvTypeprime = em.find(CvTypeprime.class, cvTypeprime.getTypeprime());
            List<CvTravailprime> cvTravailprimeListOld = persistentCvTypeprime.getCvTravailprimeList();
            List<CvTravailprime> cvTravailprimeListNew = cvTypeprime.getCvTravailprimeList();
            List<String> illegalOrphanMessages = null;
            for (CvTravailprime cvTravailprimeListOldCvTravailprime : cvTravailprimeListOld) {
                if (!cvTravailprimeListNew.contains(cvTravailprimeListOldCvTravailprime)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvTravailprime " + cvTravailprimeListOldCvTravailprime + " since its cvTypeprime field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvTravailprime> attachedCvTravailprimeListNew = new ArrayList<CvTravailprime>();
            for (CvTravailprime cvTravailprimeListNewCvTravailprimeToAttach : cvTravailprimeListNew) {
                cvTravailprimeListNewCvTravailprimeToAttach = em.getReference(cvTravailprimeListNewCvTravailprimeToAttach.getClass(), cvTravailprimeListNewCvTravailprimeToAttach.getCvTravailprimePK());
                attachedCvTravailprimeListNew.add(cvTravailprimeListNewCvTravailprimeToAttach);
            }
            cvTravailprimeListNew = attachedCvTravailprimeListNew;
            cvTypeprime.setCvTravailprimeList(cvTravailprimeListNew);
            cvTypeprime = em.merge(cvTypeprime);
            for (CvTravailprime cvTravailprimeListNewCvTravailprime : cvTravailprimeListNew) {
                if (!cvTravailprimeListOld.contains(cvTravailprimeListNewCvTravailprime)) {
                    CvTypeprime oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime = cvTravailprimeListNewCvTravailprime.getCvTypeprime();
                    cvTravailprimeListNewCvTravailprime.setCvTypeprime(cvTypeprime);
                    cvTravailprimeListNewCvTravailprime = em.merge(cvTravailprimeListNewCvTravailprime);
                    if (oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime != null && !oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime.equals(cvTypeprime)) {
                        oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime.getCvTravailprimeList().remove(cvTravailprimeListNewCvTravailprime);
                        oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime = em.merge(oldCvTypeprimeOfCvTravailprimeListNewCvTravailprime);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvTypeprime.getTypeprime();
                if (findCvTypeprime(id) == null) {
                    throw new NonexistentEntityException("The cvTypeprime with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypeprime cvTypeprime;
            try {
                cvTypeprime = em.getReference(CvTypeprime.class, id);
                cvTypeprime.getTypeprime();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypeprime with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvTravailprime> cvTravailprimeListOrphanCheck = cvTypeprime.getCvTravailprimeList();
            for (CvTravailprime cvTravailprimeListOrphanCheckCvTravailprime : cvTravailprimeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvTypeprime (" + cvTypeprime + ") cannot be destroyed since the CvTravailprime " + cvTravailprimeListOrphanCheckCvTravailprime + " in its cvTravailprimeList field has a non-nullable cvTypeprime field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvTypeprime);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypeprime> findCvTypeprimeEntities() {
        return findCvTypeprimeEntities(true, -1, -1);
    }

    public List<CvTypeprime> findCvTypeprimeEntities(int maxResults, int firstResult) {
        return findCvTypeprimeEntities(false, maxResults, firstResult);
    }

    private List<CvTypeprime> findCvTypeprimeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypeprime.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypeprime findCvTypeprime(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypeprime.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypeprimeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypeprime> rt = cq.from(CvTypeprime.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
